package com.fist.eth.flutter_plugin_eth_wallet.utils

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import org.web3j.crypto.CipherException
import org.web3j.crypto.Credentials
import org.web3j.crypto.Wallet
import org.web3j.crypto.WalletFile
import java.io.IOException

object WalletUtils {
    private val objectMapper = ObjectMapper()
    private val secureRandom = SecureRandomUtils.secureRandom

    init {
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    }

    @Throws(IOException::class, CipherException::class)
    fun loadCredentials(password: String, walletFile: WalletFile): Credentials {
        return Credentials.create(Wallet.decrypt(password, walletFile))
    }
}